#! /usr/bin/env node
// import download from 'download-git-repo'
// download('yingside/webpack-template', 'test', function (err) {
// 	console.log(err ? 'Error' : 'Success')
// })
import chalk from "chalk"

import { program } from "commander"
import templates from "./templates.js"
import figlet from 'figlet'
// import pkg from "./package.json" assert { type: "json" }
// import { readFile } from 'fs/promises'
import fs from 'fs-extra'
import logSymbols from './logSymbols.js';
import { table } from 'table';
import initAction from './initAction.js'
const config = {
  border: {
    topBody: `─`,
    topJoin: `┬`,
    topLeft: `┌`,
    topRight: `┐`,

    bottomBody: `─`,
    bottomJoin: `┴`,
    bottomLeft: `└`,
    bottomRight: `┘`,

    bodyLeft: `│`,
    bodyRight: `│`,
    bodyJoin: `│`,

    joinBody: `─`,
    joinLeft: `├`,
    joinRight: `┤`,
    joinJoin: `┼`
  }
};
// Using commonjs?
// const { table } = require('table');

const data = [
    ['0A', '0B', '0C'],
    ['1A', '1B', '1C'],
    ['2A', '2B', '2C']
];


//1.
// const pkg = JSON.parse(await readFile(new URL('./package.json', import.meta.url)))
// console.log(pkg) 
//2.
const pkg = fs.readJsonSync(new URL('./package.json', import.meta.url))
// program.version(pkg.version, '-v, --version');
//3.
// import { createRequire } from "module";
// const require= createRequire(import.meta.url);
// const pkg = require("./package.json");
// console.log(pkg) 
program.version(pkg.version,'-v, --version')

// await clone('yingside/webpack-template', 'test')

// console.log('hello')
program
.name('wsy-cli-test')
.description('一个简单的脚手架工具')
.usage('<command> [option]')
.on("--help", () => {
  console.log('help.....')
  console.log("\r\n" + chalk.greenBright.bold(figlet.textSync("GGBoo!!", {
    font: "Standard",
    horizontalLayout: "default",
    verticalLayout: "default",
    width: 80,
    whitespaceBreak: true,
  })))

  console.log(`\r\n Run ${chalk.cyan(`GGBoo <command> --help`)} for detailed usage of given command.`)
  console.log(table(data, config));
  console.log(table(data));
});
program
    .command('init <name>')
    .description('init a project')
    .option('-t, --template [template]', '输入模板名称创建项目')
    .option('-f, --force', '强制覆盖本地同名项目')
    .option('-i, --ignore', '忽略相关描述，快速创建项目')
    .action(async (name,option) => {
        // await clone('yingside/webpack-template', name)
        initAction(name,option)
        // console.log(chalk.green(`创建项目:`+name,option))
        console.log(option)
    });

    program
    .command('list')
    .description('list all templates')
    .action(async () => {
      console.log(chalk.yellowBright(logSymbols.star,'模板列表'));
      console.log(chalk.green('列出所有模板'))
      for(let i = 0;i<templates.length;i++){
       console.log((i+1)+'.'+chalk.green(templates[i].name))
      }
       
    });
// 利用commander解析命令行输入，必须写在所有内容最后面
program.parse(process.argv)
